package com.stylefeng.guns.modular.netcar.controller;

import cn.hutool.core.bean.BeanUtil;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.validater.Insert;
import com.stylefeng.guns.core.validater.Update;
import com.stylefeng.guns.modular.netcar.model.LineRangePrice;
import com.stylefeng.guns.modular.netcar.service.ILineRangePriceService;
import com.stylefeng.guns.modular.netcar.warpper.LineRangePriceWarpper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 线路超区域加价
 *
 * @author 张亚林
 * @Date 2018-10-27 09:54
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/lineRangePrice"})
@Api(value = "线路超区域加价操作相关api", tags = {"线路超区域加价操作接口"})
@Slf4j(topic = "线路超区域加价")
public class LineRangePriceController extends BaseController {
    @Autowired
    private ILineRangePriceService lineRangePriceService;
    /**
     * 分页获取线路超区域加价列表
     * @param lineRangePrice
     * @param pageNum
     * @param pageSize
     * @return	
     */
    @ApiOperation(value = "分页获取线路超区域加价列表", notes = "分页获取线路超区域加价列表", httpMethod = "POST")
    @PostMapping(value = "/pageList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    public Result pageList(
        @RequestBody @ApiParam(name = "lineRangePrice", value = "线路超区域加价对象")LineRangePrice lineRangePrice,
        @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
        @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize		
        ) {
        try {
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            pager.setObj(lineRangePrice);
            Map map=lineRangePriceService.pageListLineRangePrice(pager,getToken());
            if(!map.isEmpty()){
                List<LineRangePrice> list=(List<LineRangePrice>)map.get("rows");
                List<Map<String, Object>> mList=new ArrayList<>();
                for (LineRangePrice o:list){
                    Map map1= BeanUtil.beanToMap(o);
                    mList.add(map1);
                }
                super.warpObject(new LineRangePriceWarpper(mList));
                BeanUtil.setFieldValue(map,"rows",mList);

            }
            return Result.success(map);
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取线路超区域加价列表
     * @param lineRangePrice    
     * @return	
     */
    @ApiOperation(value = "获取线路超区域加价列表", notes = "获取线路超区域加价列表", httpMethod = "POST")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    public Result list(
        @RequestBody @ApiParam(name = "lineRangePrice", value = "线路超区域加价对象")LineRangePrice lineRangePrice) {
        try {            
            return Result.success( lineRangePriceService.selectListLineRangePrice(lineRangePrice,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 通过主键获取线路超区域加价对象
     * @param id
     * @return
     */
    @ApiOperation(value = "通过主键获取线路超区域加价对象", notes = "通过主键获取线路超区域加价对象", httpMethod = "GET")
    @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result getById(
            @ApiParam(name = "id", value = "主键ID", required = true) @RequestParam(value = "id", required = true) String  id
    ) {
        try {
            return Result.success( lineRangePriceService.findLineRangePriceById(id,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 新增线路超区域加价数据
     *
     * @param lineRangePrice 
     * @param result
     * @return
     */
    @ApiOperation(value = "新增线路超区域加价数据", notes = "新增线路超区域加价数据", httpMethod = "POST")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "新增线路超区域加价数据")
    @ResponseBody
    public Result add( @Validated(value = Insert.class) @RequestBody @ApiParam(name = "lineRangePrice ", value = "线路超区域加价对象")LineRangePrice lineRangePrice ,BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            lineRangePriceService.insertLineRangePrice(lineRangePrice,getToken());
            return Result.success();
        }catch(Exception e){
            String msg=e.getMessage();
            if(msg.indexOf("for key 'index_1'")>-1){
                msg="区域已经存在！";
            }
            return Result.error(CodeMsg.SERVER_EXCEPTION,msg);
        }
    }

    /**
     * 初始化线路超区域加价数据
     *
     * @param olId
     * @param lcId
     * @return
     */
    @ApiOperation(value = "初始化线路超区域加价数据", notes = "初始化线路超区域加价数据", httpMethod = "POST")
    @PostMapping(value = "/addInit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "初始化线路超区域加价数据")
    @ResponseBody
    public Result addInit(
            @ApiParam(name = "olId", value = "线路Id", required = true) @RequestParam(value = "olId", required = true) String  olId,
            @ApiParam(name = "lcId", value = "城市Id", required = true) @RequestParam(value = "lcId", required = true) String  lcId
    ) {
        try {
            lineRangePriceService.insertInitLineRangePrice(olId,lcId,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 更新线路超区域加价数据
     * @param lineRangePrice
     * @param result
     * @return
     */
    @ApiOperation(value = "更新线路超区域加价数据", notes = "更新线路超区域加价数据", httpMethod = "POST")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "更新线路超区域加价数据")
    @ResponseBody
    public Result update(@Validated(value = Update.class) @RequestBody @ApiParam(name = "lineRangePrice", value = "线路超区域加价对象")LineRangePrice lineRangePrice, BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            lineRangePriceService.updateLineRangePrice(lineRangePrice,getToken());
            return Result.success();
        }catch(Exception e){
            String msg=e.getMessage();
            if(msg.indexOf("for key 'index_1'")>-1){
                msg="区域已经存在！";
            }
            return Result.error(CodeMsg.SERVER_EXCEPTION,msg);
        }
    }
    /**
     * 删除线路超区域加价数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除线路超区域加价数据", notes = "删除线路超区域加价数据", httpMethod = "GET")
    @GetMapping(value = "/delBatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除线路超区域加价数据")
    @ResponseBody
    public Result delBatch(
            @ApiParam(name = "ids", value = "主键ID字符数组", required = true) @RequestParam(value = "ids", required = true) String ids
    ) {
        try {
            lineRangePriceService.deleteBatchLineRangePrice(ids.split(","),getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	/**
     * 删除线路超区域加价数据
     * @param lineRangePrice
     * @return
     */
    @ApiOperation(value = "删除线路超区域加价数据", notes = "删除线路超区域加价数据", httpMethod = "POST")
    @PostMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除线路超区域加价数据")
    @ResponseBody
    public Result del(@RequestBody @ApiParam(name = "lineRangePrice", value = "线路超区域加价对象")LineRangePrice lineRangePrice) {
        try {
            lineRangePriceService.deleteLineRangePrice(lineRangePrice,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取线路超区域加价的Key-Value列表，用于html的Select数据源
     * @param lineRangePrice
     * @return
     */
    @ApiOperation(value = "获取线路超区域加价的Key-Value列表，用于html的Select数据源", notes = "获取线路超区域加价的Key-Value列表，用于html的Select数据源", httpMethod = "POST")
    @PostMapping(value = "/optionsList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result optionsList(@RequestBody @ApiParam(name = "lineRangePrice", value = "线路超区域加价对象")LineRangePrice lineRangePrice) {
        try {
            return Result.success(lineRangePriceService.optionsLineRangePrice(lineRangePrice,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	private String getToken(){		
		return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
